package com.pda.yuanjing.pages.returnscanf;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import com.pda.yuanjing.R;
import com.pda.yuanjing.boardcast.CustomReceiver;
import com.pda.yuanjing.boardcast.ScannerInterface;
import com.pda.yuanjing.thread.CodeBeans;
import com.pda.yuanjing.tools.StringUtils;
import com.public_module.basemvp.activity.BaseActivity;
import com.public_module.dialogs.ChooseDialog;
import com.public_module.tools.DateUtils;
import com.public_module.tools.VoiceUtils;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReturnScanfActivity extends BaseActivity<ReturnScanfModel, ReturnScanfView, ReturnScanfPresent> implements ReturnScanfView {
    private String TAG = "ReturnScanfActivity";

    @BindView(R.id.id_flowlayout)
    TagFlowLayout idFlowlayout;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.tvbill)
    TextView tvbill;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.logine)
    Button logine;
    @BindView(R.id.tvtnum)
    TextView tvtnum;
    @BindView(R.id.sws)
    Switch sws;
    @BindView(R.id.ivd)
    ImageView ivd;
    @BindView(R.id.tvres)
    TextView tvres;
    @BindView(R.id.tvs)
    TextView tvs;
    @BindView(R.id.tv0)
    TextView tv0;
    @BindView(R.id.tvwait)
    TextView tvwait;

    int scanfState = 0;
    boolean flag = true;
    String bill = "TH" + DateUtils.stampToDateTime(new Date().getTime());
    ScannerInterface scanner;
    CustomReceiver customReceiver = null;
    VoiceUtils voiceUtils;
    CodeBeans codeBeans = new CodeBeans();
    int waitNum = 0;
    Thread upThread = null;
    boolean exit = false;
    List<String> strs = new ArrayList<>();


    protected void initdata() {
        tvTitle.setText("退货扫描");
        tvbill.setText(bill);
        tvtnum.setText(strs.size() + "");
        tvwait.setText(waitNum + "");
        scanner = new ScannerInterface(this);
        scanner.setOutputMode(1);

        chooseDialog = new ChooseDialog(this, "是否开启删除条码", R.style.CommentDialog, new ChooseDialog.PriorityListener() {
            @Override
            public void setActivityText() {
                chooseDialog.dismiss();
                flag = false;
                ivd.setImageResource(R.mipmap.icon_check);
            }
        });

        customReceiver = new CustomReceiver(
                new CustomReceiver.PListener1() {
                    @Override
                    public void setData(CustomReceiver.Dataa dataa) {
                        tvs.setText(dataa.getStr());
                        scanfState = dataa.getType();
                        if (scanfState == -1) {
                            tvs.setBackgroundResource(R.color.smalltext);
                        } else {
                            tvs.setBackgroundResource(R.color.green);
                        }
                    }
                },
                new CustomReceiver.PListener2() {
                    @Override
                    public void setText(String string) {
                        if (scanfState == -1) {
                            voiceUtils.voiceWarn();
                            tvres.setText("网络异常,请检查网络");
                            return;
                        }
                        if (StringUtils.checkBoxCode(string) || StringUtils.checkCaseCode(string)) {
                            CodeBeans.Code cbean = new CodeBeans.Code();
                            cbean.setStr(string);
                            cbean.setFlag(flag);
                            codeBeans.addCode(cbean);
                            tvwait.setText(codeBeans.getCodes().size() + "");
                        } else {
                            voiceUtils.voiceWarn();
                            tvres.setText("数码长度不符");
                        }
                    }
                });
        IntentFilter filter = new IntentFilter();
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        filter.addAction(ScannerInterface.RES_ACTION);
        registerReceiver(customReceiver, filter);
        voiceUtils = new VoiceUtils(this);
        upThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!exit) {
                    if (codeBeans!=null && codeBeans.getCodes().size() > 0) {
                        if (codeBeans.getCodes().get(0).isFlag()) {
                            presenter.addCode(bill, codeBeans);
                        } else {
                            presenter.deleteCode(bill, codeBeans);
                        }
                    }
                }
            }
        });
        upThread.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_scanf);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
        }
        if (scanner != null) {
            scanner.setOutputMode(0);
        }
        exit = true;
        upThread = null;
    }

    @Override
    public ReturnScanfModel createModel() {
        return new ReturnScanfModelImpl(this);
    }

    @Override
    public ReturnScanfView createView() {
        return this;
    }

    @Override
    public ReturnScanfPresent createPresenter() {
        return new ReturnScanfPresent();
    }


    @OnClick({R.id.iv_title_left, R.id.logine, R.id.ivd})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.ivd:
                if (flag) {
                    chooseDialog.show();
                } else {
                    flag = true;
                    ivd.setImageResource(R.mipmap.icon_nocheck);
                }
                break;
            case R.id.logine:
                if (scanfState == -1) {
                    voiceUtils.voiceWarn();
                    tvres.setText("网络异常,请检查网络");
                    return;
                }
                if (StringUtils.checkBoxCode(loginId.getText().toString()) || StringUtils.checkCaseCode(loginId.getText().toString())) {
                    CodeBeans.Code cbean = new CodeBeans.Code();
                    cbean.setStr(loginId.getText().toString());
                    cbean.setFlag(flag);
                    codeBeans.addCode(cbean);
                } else {
                    voiceUtils.voiceWarn();
                    tvres.setText("数码长度不符");
                }
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void updateView(String str, int flag) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loginId.setText("");
                if (flag == 1) {
                    strs.add(0,str);
                } else {
                    strs.remove(str);
                }
                idFlowlayout.setAdapter(new TagAdapter<String>(strs) {
                    @Override
                    public View getView(FlowLayout parent, int position, String s) {
                        TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.tv, idFlowlayout, false);
                        tv.setText(s);
                        return tv;
                    }
                });
                tvtnum.setText(strs.size() + "");
                tvres.setText("数码处理完成");
                tvwait.setText(codeBeans.getCodes().size() + "");
            }
        });
    }

    @Override
    public void showres(String str) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                voiceUtils.voiceWarn();
                tvres.setText(str);
                tvwait.setText(codeBeans.getCodes().size() + "");
            }
        });
    }
}
